//
//  NewMissonListDeatilHeadView.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2018/4/13.
//  Copyright © 2018年 zjhcsoftios. All rights reserved.
//

#import "NewMissonListDeatilHeadView.h"
#import "NewMissionListDetailHeaderModel.h"
#import "NewMissionListDetailHeaderCell.h"
#import "NetworkAddress.h"
#import "iSeeNetworkRequest.h"
#import "SVProgressHUD.h"
#import <MJExtension/MJExtension.h>

@interface NewMissonListDeatilHeadView()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *headCollectionView;

@property (nonatomic, strong) NSMutableArray *headerList;

@end

@implementation NewMissonListDeatilHeadView

static NSString *newMissionListDetailHeaderCellIdentify = @"newMissionListDetailHeaderCellIdentify";

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _headerList = [NSMutableArray array];
        [self addCollectionView];
    }
    return self;
}

- (void)addCollectionView {
    UICollectionViewFlowLayout *headLayout = [[UICollectionViewFlowLayout alloc] init];
    headLayout.itemSize =  CGSizeMake(42, 42);
    headLayout.minimumInteritemSpacing = 7.5;
    headLayout.minimumLineSpacing = 9;
    
    _headCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(7.5, 9, kScreenWidth - 15, 60 - 18) collectionViewLayout:headLayout];
    _headCollectionView.dataSource = self;
    _headCollectionView.delegate = self;
    _headCollectionView.backgroundColor = [UIColor clearColor];
    [_headCollectionView registerNib:[UINib nibWithNibName:@"NewMissionListDetailHeaderCell" bundle:nil] forCellWithReuseIdentifier:newMissionListDetailHeaderCellIdentify];
    [self addSubview:_headCollectionView];
}

- (void)setOrdorId:(NSString *)ordorId {
    _ordorId = ordorId;
    
    [self getHeadList];
}

#pragma mark 网络请求
- (void)getHeadList {
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setObject:_ordorId forKey:@"orderId"];
    [_headerList removeAllObjects];
    
    [iSeeNetworkRequest getWithHeaderUrl:kFormat(@"%@%@",NMarketMainUrl,ZPNewMissionListDetailHeadList) params:parma success:^(id object) {
        [self.headerList addObjectsFromArray:[NewMissionListDetailHeaderModel mj_objectArrayWithKeyValuesArray:object[@"body"]]];
        [self.headCollectionView reloadData];
    } failure:^(NSError *error) {
        [self.headCollectionView reloadData];
        NSDictionary *dic = error.userInfo;
        NSString *msg = [dic objectForKey:@"msg"];
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}

- (void)changeMark:(NewMissionListDetailHeaderModel *) model IndexPath:(NSIndexPath *) indexPath {
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setObject:_ordorId forKey:@"orderId"];
    [parma setObject:model.labelId forKey:@"labelId"];
    
    [iSeeNetworkRequest getWithHeaderUrl:kFormat(@"%@%@",NMarketMainUrl,ZPNewMissionListDetailChangeMark) params:parma success:^(id object) {
        NewMissionListDetailHeaderModel *  newModel = [NewMissionListDetailHeaderModel mj_objectWithKeyValues:object[@"body"]];
        [self changeMode:newModel IndexPath:indexPath];
    } failure:^(NSError *error) {
        NSDictionary *dic = error.userInfo;
        NSString *msg = [dic objectForKey:@"msg"];
        [SVProgressHUD showErrorWithStatus:msg];

    }];

}

- (void)changeMode:(NewMissionListDetailHeaderModel *) model IndexPath:(NSIndexPath *) indexPath {
    [_headerList replaceObjectAtIndex:indexPath.row withObject:model];
    
    [_headCollectionView reloadData];
}

#pragma mark collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _headerList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewMissionListDetailHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:newMissionListDetailHeaderCellIdentify forIndexPath:indexPath];
    cell.model = _headerList[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NewMissionListDetailHeaderModel *model = _headerList[indexPath.row];
    if ([model.gdzd isEqualToString:@"2"]) {
        [self changeMark:model IndexPath:indexPath];
    }
}

@end
